package com.example.gravitysituation;

import android.os.Bundle;
import java.util.Random;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class CatapultSimulation extends Activity {

        public static Integer goal = 0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_catapult_simulation);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.catapult_simulation, menu);
                
                Random randomGenerator = new Random();
                goal = randomGenerator.nextInt(400) + 400;
                TextView goalView = (TextView) findViewById(R.id.goalValueText);
                goalView.setText(goal.toString());
                AnimatedView av = (AnimatedView) findViewById(R.id.anim_view);
                av.setGoal(goal);
                
                return true;
        }

    public Double calcDist( Double height, Double velocity, Double angle) {
        Double acceleration = -9.80665;

        Double xvel;
        Double yvel;
        
        xvel = Math.cos(Math.toRadians(angle))*velocity;
        yvel = Math.sin(Math.toRadians(angle))*velocity;
        
        height = height*(-1);
        //Double time_pos = (Math.sqrt(2 * acceleration * height + yvel * yvel) - yvel)/acceleration;
        Double time_neg = -(Math.sqrt(2 * acceleration * height + yvel * yvel) + yvel)/acceleration;
        
        Double distance = time_neg * xvel;
        
        return distance;
    }
    
    public void runCatapult(View view) {
       // EditText heightText = (EditText) findViewById(R.id.heightVal);
        EditText velocityText = (EditText) findViewById(R.id.velVal);
        EditText angleText = (EditText) findViewById(R.id.angleVal);
        
       // String heightString = heightText.getText().toString();
        String velocityString = velocityText.getText().toString();
        String angleString = angleText.getText().toString();
        
        
      //  Double height = Double.parseDouble(heightString);
        Double velocity = Double.parseDouble(velocityString);
        Double angle = Double.parseDouble(angleString);
        
        Double distance = calcDist(new Double(0.0), velocity, angle);
        Double error = distance - (Double) goal.doubleValue();
        
        String distanceString = distance.toString().substring(0, 4);
        //String errorString = error.toString().substring(0, 4);
        
        //TextView resultView = (TextView) findViewById(R.id.resultText);
        //TextView errorText = (TextView) findViewById(R.id.errorText);
 
        //resultView.setText(distanceString);
        //errorText.setText(errorString);
        
        AnimatedView av = (AnimatedView) findViewById(R.id.anim_view);
        
        av.Run(new Double(0.0), velocity, angle, goal);
        
        
        return;
        
    }

        
}